package com.example.calendar.ui.screens.category

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.FilterList
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.example.calendar.ui.Screen
import com.example.calendar.ui.screens.tasks.MockTask
import com.example.calendar.ui.screens.tasks.TaskItem
import com.example.calendar.ui.screens.tasks.generateMockTasks
import java.time.LocalDate

/**
 * Screen for displaying category details and tasks within that category
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CategoryDetailScreen(
    categoryId: String,
    navController: NavController
) {
    var showDeleteDialog by remember { mutableStateOf(false) }
    
    // Mock category data
    val category = remember {
        MockCategory(
            id = categoryId,
            name = when (categoryId) {
                "1" -> "Work"
                "2" -> "Personal"
                "3" -> "Shopping"
                "4" -> "Health"
                "5" -> "Finance"
                else -> "Unknown"
            },
            color = when (categoryId) {
                "1" -> 0xFF2196F3
                "2" -> 0xFF4CAF50
                "3" -> 0xFFFF9800
                "4" -> 0xFFE91E63
                "5" -> 0xFF9C27B0
                else -> 0xFF607D8B
            }
        )
    }
    
    // Mock tasks for this category
    val allTasks = remember { generateMockTasks() }
    val tasksInCategory = allTasks.take(3) // Just use first 3 tasks as a mock
    
    // Task completion states
    var taskCompletionStates by remember { 
        mutableStateOf(tasksInCategory.associate { it.id to it.isCompleted }.toMutableMap()) 
    }
    
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(category.name) },
                navigationIcon = {
                    IconButton(onClick = { navController.navigateUp() }) {
                        Icon(
                            imageVector = Icons.Default.ArrowBack,
                            contentDescription = "Back"
                        )
                    }
                },
                actions = {
                    IconButton(onClick = { showDeleteDialog = true }) {
                        Icon(
                            imageVector = Icons.Default.Delete,
                            contentDescription = "Delete Category"
                        )
                    }
                }
            )
        },
        floatingActionButton = {
            FloatingActionButton(
                onClick = {
                    navController.navigate(Screen.CategoryEdit.createRoute(categoryId))
                }
            ) {
                Icon(
                    imageVector = Icons.Default.Edit,
                    contentDescription = "Edit Category"
                )
            }
        }
    ) { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
        ) {
            // Category header
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                // Category color
                Box(
                    modifier = Modifier
                        .size(64.dp)
                        .clip(CircleShape)
                        .background(Color(category.color))
                )
                
                Spacer(modifier = Modifier.height(8.dp))
                
                // Category name
                Text(
                    text = category.name,
                    style = MaterialTheme.typography.headlineMedium,
                    textAlign = TextAlign.Center
                )
                
                Spacer(modifier = Modifier.height(4.dp))
                
                // Task count
                Text(
                    text = "${tasksInCategory.size} tasks",
                    style = MaterialTheme.typography.bodyLarge,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
            
            Divider()
            
            // Tasks section
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp)
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 16.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = "Tasks",
                        style = MaterialTheme.typography.titleLarge,
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier.weight(1f)
                    )
                    
                    IconButton(onClick = { /* Filter tasks */ }) {
                        Icon(
                            imageVector = Icons.Default.FilterList,
                            contentDescription = "Filter Tasks"
                        )
                    }
                }
                
                if (tasksInCategory.isEmpty()) {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(vertical = 32.dp),
                        contentAlignment = Alignment.Center
                    ) {
                        Text(
                            text = "No tasks in this category",
                            style = MaterialTheme.typography.bodyLarge,
                            color = MaterialTheme.colorScheme.onSurfaceVariant
                        )
                    }
                } else {
                    LazyColumn {
                        items(tasksInCategory) { task ->
                            TaskItem(
                                task = task,
                                isCompleted = taskCompletionStates[task.id] ?: task.isCompleted,
                                onTaskClick = {
                                    navController.navigate(Screen.TaskDetail.createRoute(task.id))
                                },
                                onCompletionToggle = { isComplete ->
                                    taskCompletionStates[task.id] = isComplete
                                }
                            )
                            Spacer(modifier = Modifier.height(8.dp))
                        }
                        
                        // Space for FAB
                        item {
                            Spacer(modifier = Modifier.height(80.dp))
                        }
                    }
                }
            }
        }
        
        // Delete confirmation dialog
        if (showDeleteDialog) {
            AlertDialog(
                onDismissRequest = { showDeleteDialog = false },
                title = { Text("Delete Category") },
                text = { Text("Are you sure you want to delete ${category.name}? This will not delete the tasks in this category.") },
                confirmButton = {
                    TextButton(
                        onClick = {
                            // Delete logic would go here
                            showDeleteDialog = false
                            navController.navigateUp()
                        }
                    ) {
                        Text("Delete")
                    }
                },
                dismissButton = {
                    TextButton(
                        onClick = { showDeleteDialog = false }
                    ) {
                        Text("Cancel")
                    }
                }
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun CategoryDetailScreenPreview() {
    MaterialTheme {
        CategoryDetailScreen(
            categoryId = "1",
            navController = rememberNavController()
        )
    }
} 